Imports System.Data.SqlClient
''' <nombre>
''' Telefono
''' </nombre>
Public Class cls_telefono_

    Implements ICloneable

#Region "Variables ..."
    Private int_idtelefono As Integer = 0
    Private str_numero As String = ""
    Private str_interno As String = ""
    Private int_idtipotelefono As Integer = 0
    Private int_idtercero As Integer = 0
    Private bit_primario As Boolean = False

    Private obj_tipotelefono As cls_tipotelefono_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_telefono_) = Nothing
#End Region

    Public Overrides Function ToString() As String

        If Not IsNothing(tipotelefono) Then
            Select Case str_formatoCombos
                Case "Nombre#########(15) - sigla(5)"
                    Return numero.PadRight(15) & " - " & tipotelefono.ToString.PadRight(5)
                Case "sigla(5) - Nombre#########(15)"
                    Return tipotelefono.ToString.PadRight(5) & " - " & numero.PadRight(15)
                Case "Nombre - sigla"
                    Return numero & " - " & tipotelefono.ToString
                Case "sigla - Nombre"
                    Return tipotelefono.ToString & " - " & numero
                Case Else
                    Return tipotelefono.ToString & " - " & numero
            End Select
        Else
            Select Case str_formatoCombos
                Case "Nombre#########(15) - sigla(5)"
                    Return numero.PadRight(15)
                Case "sigla(5) - Nombre#########(15)"
                    Return numero.PadRight(15)
                Case "Nombre - sigla"
                    Return numero
                Case "sigla - Nombre"
                    Return numero
                Case Else
                    Return numero
            End Select
        End If
        
    End Function

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idtelefono
        End Get
    End Property
    ''' <id>
    ''' True
    ''' </id>
    Property idtelefono() As Integer
        Get
            idtelefono = int_idtelefono
        End Get
        Set(ByVal value As Integer)
            int_idtelefono = value
        End Set
    End Property
    ''' <nombre>
    ''' Numero
    ''' </nombre>
    Property numero() As String
        Get
            numero = str_numero
        End Get
        Set(ByVal value As String)
            str_numero = value
        End Set
    End Property
    ''' <nombre>
    ''' Interno
    ''' </nombre>
    Property interno() As String
        Get
            interno = str_interno
        End Get
        Set(ByVal value As String)
            str_interno = value
        End Set
    End Property
    ''' <nombre>
    ''' Identificador
    ''' </nombre>
    Property idtipotelefono() As Integer
        Get
            idtipotelefono = int_idtipotelefono
        End Get
        Set(ByVal value As Integer)
            int_idtipotelefono = value
        End Set
    End Property
    ''' <nombre>
    ''' Identificador
    ''' </nombre>
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    Property primario() As Boolean
        Get
            primario = bit_primario
        End Get
        Set(ByVal value As Boolean)
            bit_primario = value
        End Set
    End Property
    ''' <importar>
    ''' True
    ''' </importar>
    Property tipotelefono() As cls_tipotelefono_
        Get
            If IsNothing(obj_tipotelefono) Then
                obj_tipotelefono = New cls_tipotelefono_
                If Not obj_tipotelefono.Buscar(idtipotelefono) Then
                    obj_tipotelefono = Nothing
                End If
            End If
            tipotelefono = obj_tipotelefono
        End Get
        Set(ByVal value As cls_tipotelefono_)
            obj_tipotelefono = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_telefono_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_telefono_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_telefono_)
        While obj_Reader.Read()
            Dim Elemento As New cls_telefono_
            Elemento.int_idtelefono = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtelefono")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
                Elemento.str_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("interno"))) = False Then
                Elemento.str_interno = (obj_Reader.GetValue(obj_Reader.GetOrdinal("interno")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtipotelefono"))) = False Then
                Elemento.int_idtipotelefono = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtipotelefono")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("primario"))) = False Then
                Elemento.bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("primario")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Public Sub CargarVariables(ByVal obj_Reader As SqlDataReader, Optional read As Boolean = True)
        Dim tabla As String = ""
        If read Then
            obj_Reader.Read()
        Else
            tabla = "telefono_"
        End If

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtelefono"))) = False Then
            int_idtelefono = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtelefono")))
        End If

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "numero"))) = False Then
            str_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "numero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "interno"))) = False Then
            str_interno = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "interno")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtipotelefono"))) = False Then
            int_idtipotelefono = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtipotelefono")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtercero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "primario"))) = False Then
            bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "primario")))
        End If
        If Not read Then
            tipotelefono = New cls_tipotelefono_
            tipotelefono.CargarVariables(obj_Reader, False)
        End If

    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__Nuevo"
                .Parameters.Add("@numero", SqlDbType.NVarChar).Value = str_numero
                .Parameters.Add("@interno", SqlDbType.NVarChar).Value = str_interno
                .Parameters.Add("@idtipotelefono", SqlDbType.Int).Value = IIf(int_idtipotelefono = 0, DBNull.Value, int_idtipotelefono)
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__Modificar"
                .Parameters.Add("@idtelefono", SqlDbType.Int).Value = int_idtelefono
                .Parameters.Add("@numero", SqlDbType.NVarChar).Value = str_numero
                .Parameters.Add("@interno", SqlDbType.NVarChar).Value = str_interno
                .Parameters.Add("@idtipotelefono", SqlDbType.Int).Value = IIf(int_idtipotelefono = 0, DBNull.Value, int_idtipotelefono)
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idtelefono As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__Eliminar"
                .Parameters.Add("@idtelefono", SqlDbType.Int).Value = idtelefono
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idtelefono As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__Buscar"
                .Parameters.Add("@idtelefono", SqlDbType.Int).Value = idtelefono
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidtercero(ByVal idtercero As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidtercero = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__CargarXidtercero"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = idtercero
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidtercero = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidtercero los registros de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidterceroprimario(ByVal idtercero As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidterceroprimario = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telefono__CargarXidterceroprimario"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = idtercero
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                CargarXidterceroprimario = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidterceroprimario los registros de telefono_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_telefono_Clon As New cls_telefono_
        obj_telefono_Clon.int_idtelefono = Me.int_idtelefono
        obj_telefono_Clon.str_numero = Me.str_numero
        obj_telefono_Clon.str_interno = Me.str_interno
        obj_telefono_Clon.int_idtipotelefono = Me.int_idtipotelefono
        obj_telefono_Clon.int_idtercero = Me.int_idtercero
        obj_telefono_Clon.bit_primario = Me.bit_primario
        obj_telefono_Clon.col_Items = Me.col_Items
        Return obj_telefono_Clon
    End Function

End Class
